$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        tabBar:null,
        cloneTab:null,
        init:function () {
            this.tabBar = $('.tab-content').eq(0);
            this.cloneTab = $('.tab-pane').eq(1).clone();
            this.funObj().formInit();
            this.funObj().fillData();
            this.listener();
        },
        listener:function () {
            var $this = this;
            //新增tab
            $('#add').click(function () {
                $this.funObj().addTabAction();
            });

        },
        funObj:function () {
            var $this = this;
            return {
                formInit:function () {
                    $('.xh_form').each(function () {
                        var form = new xhForm($(this));
                        form.parse();
                        $(this).find('[name=iInterviewer]').select2();
                        form.bindRequired($(this).find('.saveBtn'));
                        $(this).on('click','.saveBtn',function () {
                            $this.funObj().submitForm(form);
                        })
                    });

                },
                submitForm:function (form) {
                    form.submit('hrm/resource/saveInterview',{},function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                        window.top.xhTop.closeModal();
                    })
                },
                addTabAction:function () {
                    //add li
                    var index = $('.tab-bar li').length;
                    var li = '<li class=""><a href="#tab_'+index+'" data-toggle="tab">面试('+index+')</a></li>';
                    $('.tab-bar li').eq(index-1).before(li);
                    //add tab
                    var tabDom = $this.cloneTab.clone();
                    $('.tab-content').append(tabDom);
                    tabDom.attr('id','tab_'+index);
                    //form
                    var formDom = tabDom.find('.xh_form');
                    var form = new xhForm(formDom);
                    form.parse();
                    formDom.find('[name=iInterviewer]').select2();
                    formDom.find('[name=iIndex]').val(index);
                    form.bindRequired(formDom.find('.saveBtn'));
                    //save event
                    formDom.on('click','.saveBtn',function () {
                        $this.funObj().submitForm(form);
                    })
                },
                fillData:function () {
                    if (info) {
                        if (maxIndex >= 4) {
                            for (var i = 0; i< maxIndex - 3 ; i++){
                                $this.funObj().addTabAction();
                            }
                        }
                        if (activeTabIndex > 1) {
                            $('.tab-bar a').eq(activeTabIndex -1).click();
                        }
                        $.each(info,function (i,row) {
                            var index = row['iIndex'];
                            var tab = $('#tab_'+index);
                            var formDom = tab.find('.xh_form');
                            var form = new xhForm(formDom);
                            form.fill(row);
                            formDom.find('[name=iInterviewer]').val(row['iInterviewer']).trigger('change');
                        })
                    }
                }
            }
        }
    }
};